package me.zhengjie.spider.util.pipeline;

import lombok.Data;
import me.zhengjie.spider.comic.domain.SpiderComic;
import me.zhengjie.spider.comic.service.SpiderComicService;
import me.zhengjie.spider.common.SpiderConstant;
import me.zhengjie.spider.domain.comic.ComicBean;
import me.zhengjie.spider.domain.comic.ComicRuleBean;
import me.zhengjie.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.Objects;

@Data
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SpiderComicGeneralPipeline implements Pipeline {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SpiderComicService spiderComicService;

    private ComicBean bean;
    private ComicRuleBean ruleBean;
    @Override
    public void process(ResultItems resultItems, Task task) {
        List<String> altList = resultItems.get(SpiderConstant.ALT);
        List<String> hrefList = resultItems.get(SpiderConstant.HREF);
        List<String> imgList = resultItems.get(SpiderConstant.IMG);
        for (int i = 0; i < altList.size(); i++) {
            if (StringUtils.isEmpty(altList.get(i)) || StringUtils.isEmpty(hrefList.get(i)) || StringUtils.isEmpty(imgList.get(i)) || Objects.equals(altList.get(i), SpiderConstant.SHAP)) {
                continue;
            }
            SpiderComic spiderComic = new SpiderComic();
            spiderComic.setAlt(altList.get(i));
            spiderComic.setHref(hrefList.get(i));
            spiderComic.setImg(imgList.get(i));
            spiderComic.setWebId(this.ruleBean.getWebId());
            spiderComic.setDeletedFlag(false);
            String redisKey = String.format(SpiderConstant.SPIDER_COMIC_KEY, spiderComic.getWebId().toString());
            String item = this.ruleBean.getWebId() + SpiderConstant.COLON + spiderComic.getHref();
            if (!this.redisUtils.hHasKey(redisKey, item)) {
                this.redisUtils.hset(redisKey, item, null);
                spiderComicService.create(spiderComic);
            }
        }
    }
}
